import datetime

import joblib
import pandas as pd
from sklearn.metrics import roc_auc_score

from utils.common import get_path
from utils.feature_engineering import feature_processing,feature_extra
from utils.log import Logger
import numpy as np

class ChurnPrediction:
    def __init__(self,data_path,model_path,scalar,encoder_dict):
        # 1.2 拼接日志文件名
        logfile_name = 'predict_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        # 1.3 创建日志对象
        self.logfile = Logger("../", logfile_name).get_logger()
        self.logfile.info("开始创建电力负荷项目的模型预测对象")
        self.data_source = pd.read_csv(get_path(data_path))
        self.model = joblib.load(model_path)
        self.scalar = scalar
        self.encoder_dict = encoder_dict

    def prediction(self):
        data = self.data_source.copy()
        x = feature_extra(data)


        y = data[['Attrition']]

        x,_,_ = feature_processing(x,is_train=False,scaler=self.scalar,encoder_dict=self.encoder_dict)
        y_pred = self.model.predict(x)
        y_pred_proba = self.model.predict_proba(x)[:, 1]
        auc = roc_auc_score(y,y_pred_proba)
        self.logfile.info(f"AUC: {auc}")
        print(f"预测AUC:{auc}")

